//
//  CycleImages.m
//  10-CustomCell-易车
//
//  Created by dllo on 16/7/28.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "CycleImages.h"

@interface CycleImages ()
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPage;
@end
@implementation CycleImages

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    
    if (page == 0) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _imagesPage.currentPage = _imagesPage.numberOfPages - 1;
        
    } else if (page == _imagesArray.count - 1) {
        
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
    
}

- (void)imagePageChange:(UIPageControl *)pageControl
{
    
    
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
   
    
}
#pragma mark -实现初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        //self.backgroundColor = [UIColor blackColor];
        
        
        [self createScrollView:frame];
               [self createImagespage:frame];
        
    }
    return self;
    
}
#warning 初始化中创建ScrollView
- (void)createScrollView:(CGRect)frame
{
   
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
    _cycleScrollView.backgroundColor = [UIColor cyanColor];
    [self addSubview:_cycleScrollView];
    
}
#warning 初始化中创建PageControl 后期需要添加Page的位置改变, 这里是默认位置
- (void)createImagespage:(CGRect)frame
{
    
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 30)];
    _imagesPage.numberOfPages = 5;
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 30 / 2);
    //_imagesPage.backgroundColor = [UIColor blackColor];
    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_imagesPage];
}
#pragma mark -实现自己定义的数组方法
- (void)setImagesWithArray:(NSArray *)array
{
    
    NSArray *imagesArray = [self handleArray:array];
    
    self.imagesArray = [NSArray arrayWithArray:imagesArray];
   
    [self createAllImageViewsWithImageArray:imagesArray];
    
    
    _imagesPage.numberOfPages = array.count;//
    // 设置 contentSize
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
   
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    
    
    
}
- (NSArray *)handleArray:(NSArray *)array
{
    
    
    id firstObject = [array firstObject];
    
    NSMutableArray *imagesArray = [NSMutableArray array];
   
    if ([firstObject isKindOfClass:[NSString class]]) {
       
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
            NSLog(@"%@", image);
            
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    
    UIImage *firstImage = [imagesArray firstObject];
    
    UIImage *lastImage = [imagesArray lastObject];
    
    [imagesArray insertObject:lastImage atIndex:0];
   
    [imagesArray addObject:firstImage];
    return [imagesArray copy];
}
- (void)createAllImageViewsWithImageArray:(NSArray *)imagesArray
{
    
        for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
}
}
@end
